package santeclair.connexion.dao.technique.connexionLocale.hibernate;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import santeclair.connexion.constantes.ConnexionConstantes;
import santeclair.connexion.dao.technique.connexionLocale.ConnexionLocaleDao;
import santeclair.connexion.domaine.technique.connexionLocale.AssureLocal;
import santeclair.connexion.domaine.technique.connexionLocale.CriteresIdentification;
import santeclair.lunar.framework.dao.jpa.impl.FwkGenericJpaDAO;

/**
 * @author pchaussalet
 */
@Repository
public class ConnexionLocaleDaoImpl extends FwkGenericJpaDAO<AssureLocal> implements ConnexionLocaleDao {

    @PersistenceContext(unitName = ConnexionConstantes.PERSISTENCE_UNIT_NAME)
    private EntityManager entityManager;

    @Override
    public Class<AssureLocal> getEntityClass() {
        return AssureLocal.class;
    }

    @Override
    public EntityManager getEntityManager() {
        return entityManager;
    }

    /** {@inheritDoc} */
    @Transactional(readOnly = true)
    public List<AssureLocal> rechercherAssuresLocaux(CriteresIdentification criteresIdentification) {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<AssureLocal> q = cb.createQuery(AssureLocal.class);
        Root<AssureLocal> root = q.from(AssureLocal.class);
        Predicate p = cb.equal(root.get("organisme"), criteresIdentification.getOrganisme());

        if (criteresIdentification.getNumeroContrat().trim().length() > 0) {
            p = cb.and(p, cb.equal(root.get("numContrat"), criteresIdentification.getNumeroContrat().trim()));
        } else {
            criteresIdentification.setNumeroContrat(null);
        }
        if (criteresIdentification.getNom().trim().length() > 0) {
            p = cb.and(p, cb.equal(root.get("nom"), criteresIdentification.getNom().trim()));
        } else {
            criteresIdentification.setNom(null);
        }
        if (criteresIdentification.getPrenom().trim().length() > 0) {
            p = cb.and(p, cb.equal(root.get("prenom"), criteresIdentification.getPrenom().trim()));
        } else {
            criteresIdentification.setPrenom(null);
        }
        if (criteresIdentification.getDateNaissance().trim().length() > 0) {
            p = cb.and(p, cb.equal(root.get("dateNaissance"), criteresIdentification.getDateNaissance().trim()));
        } else {
            criteresIdentification.setDateNaissance(null);
        }

        List<AssureLocal> listeAssures = entityManager.createQuery(q.select(root).where(p)).getResultList();

        return listeAssures;
    }
}
